package com.fancy.middleware.common.serializer;

import com.fancy.middleware.common.utils.ProtoStuffUtils;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * 使用Protostuff对value进行编解码。
 */
public class ProtostuffRedisSerializer<T> implements RedisSerializer<T> {

    private Class<T> clazz;

    public ProtostuffRedisSerializer(Class<T> clazz) {
        super();
        this.clazz = clazz;
    }
    @Override
    public byte[] serialize(T t) throws SerializationException {
        return ProtoStuffUtils.serialize(t);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        return ProtoStuffUtils.deserialize(bytes, clazz);
    }
}
